import { GameConfig } from "../../src/config/GameConfig";
import { RoomConfig } from "../../src/config/RoomConfig";
import { Logger } from "../../src/engine/utils/Logger";
import { JhaoProto } from "../../src/lib/net/protobuf/jhaoproto";
import { RoomTemplateDao } from "../dao/RoomTemplateDao";
import { RoomTemplateEntity } from "../entity/RoomTemplateEntity";
import { HallServer_SocketMsgHandler } from "../HallServer_SocketMsgHandler";

export class RoomTemplateModel {

    private static roomTemplateMap: Map<number, JhaoProto.IRoomTemplate> = new Map<number, JhaoProto.IRoomTemplate>();

    public id: number;
    public name: string;
    public game_type: number;
    public clubid: number;
    public creator: number
    public roomType: number
    public conf_base: JhaoProto.IRoomConfigBase
    public config_game: object

    public static async init() {
        let rows: Array<RoomTemplateEntity> = await RoomTemplateDao.get_all_room_template()
        this.roomTemplateMap.clear()
        for (let i = 0; i < rows.length; i++) {
            this.initOneRoomTemplate(rows[i])
        }
    }

    public static initOneRoomTemplate(obj: RoomTemplateEntity) {
        let model: JhaoProto.IRoomTemplate = {
            id: obj.id,
            name: obj.name,
            gameType: obj.game_type,
            clubid: obj.clubid,
            creator: obj.creator,
            roomType: obj.roomType,
            roomConfigBase: JSON.parse(obj.conf_base) as JhaoProto.IRoomConfigBase,
        }
        if (model.gameType == GameConfig.Game_Type.Mj) {
            model.roomConfigMj = JSON.parse(obj.config_game) as JhaoProto.IRoomConfigMj
        }
        this.roomTemplateMap.set(model.id, model)
        return model
    }

    public static getRoomTemplateListByClub(clubid: number) {
        let result: Array< JhaoProto.IRoomTemplate> = []
        this.roomTemplateMap.forEach((model:  JhaoProto.IRoomTemplate, key: number) => {
            if (model.clubid == clubid) {
                result.push(model)
            }
        });
        return result;
    }

    // public static getRoomTemplateByZiyou(level: number) {
    //     let result: Array<RoomTemplateModel> = []
    //     this.roomTemplateMap.forEach((model: RoomTemplateModel, key: number) => {
    //         if (model.roomType == RoomConfig.Room_Type.Ziyou) {
    //             // if(model.conf["level"] == level){
    //             //     result.push(model)
    //             // }
    //         }
    //     });
    //     return result;
    // }

    public static deleteRoomTemplate(id: number) {
        this.roomTemplateMap.delete(id)
    }
}